/**
 * 
 */
package org.springside.examples.miniweb.service.report;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.examples.miniweb.dao.report.ColdPumpReportDao;
import org.springside.examples.miniweb.entity.report.ColdPumpReport;

/**
 * @author Eric 2012-3-6
 * 
 * @since 6.0
 * 
 */
//Spring Bean的标识.
@Component
//默认将类中的所有public函数纳入事务管理.
@Transactional(readOnly = true)
public class ColdPumpReportManager<T> {

	private ColdPumpReportDao coldDao;
	
	/**
	 * @param coldDao the coldDao to set
	 */
	@Autowired
	public void setColdDao(ColdPumpReportDao coldDao) {
		this.coldDao = coldDao;
	}

	@SuppressWarnings("unchecked")
	public List<ColdPumpReport> getAllReports() {
		return (List<ColdPumpReport>) coldDao.findAll(new Sort(Direction.ASC, "id"));
	}
	
	@Transactional(readOnly = false)
	public void saveReport(ColdPumpReport report) {
		if(report.getInputRepository() != null
				&& report.getColdDismantle() != null
				&& report.getEmptyDismantle()!= null
				&& report.getColdClean() != null
				&& report.getEmptyClean() != null
				&& report.getColdAssemble() != null
				&& report.getPistonAssemble() != null
				&& report.getEmptyAssemble() != null
				&& report.getDownTempTest() != null
				&& report.getGasTest() != null
				&& report.getFinishPackage() != null
				&& report.getOutputRepository() != null
				){
			report.setStatus("finish");
		}else{
			report.setStatus("");
		}
		coldDao.save(report);
	}
	
	@Transactional(readOnly = false)
	public ColdPumpReport getOneById (Long id){
		return coldDao.findById(id);
	}
	
	@Transactional(readOnly = false)
	public List<ColdPumpReport> getReportByCustomerName (String customerName){
		return coldDao.findByCustomerName(customerName);
	}
	
	@Transactional(readOnly = false)
	public List<ColdPumpReport> getReportByUserName (String userName){
		return coldDao.findByAssignedUser(userName);
	}
	
	@Transactional(readOnly = false)
	public void deleteReport(Long id) { 
		coldDao.delete(id);
	}
}
